<template>
  <transition name="move">
    <div v-show="showFlag" class="food" ref="foodscroll">
        <div class="food-content">
            <div class="image-header">
                <img :src="food.image">
                <div class="back" @click="hide">
                    <i class="icon-arrow_lift"></i>
                </div>
            </div>
            <div class="content">
                <h2 class="name">{{food.name}}</h2>
                <div class="extra">
                    <span class="count">月售{{food.sellCount}}份</span>
                    <span class="rating">好评率{{food.rating}}%</span>
                </div>
                <div class="price">
                    <span class="now">￥{{food.price}}</span>
                    <span class="old" v-show="food.oldPrice">￥{{food.oldPrice}}</span>
                </div>
                <div class="cartcontrol-wrapper">
                    <cartcontrol :food="food" @cart-add="cartAddFood"></cartcontrol>
                </div>
                <transition name="fade">
                    <div class="cartcontrol-botton" v-show="!this.food.count || this.food.count === 0" @click="addFirst" @click.stop @click.prevent>
                        <div class="botton">加入购物车</div>
                    </div>
                </transition>
            </div>
            <split v-show="food.info"></split>
            <div class="info" v-show="food.info">
                <h1 class="title">商品介绍</h1>
                <p class="text">{{food.info}}</p>
            </div>
            <split></split>
            <div class="rating">
                <h1 class="title">商品评价</h1>
                <ratingselect :selectType="selectType" :onlyContent="onlyContent" :desc="desc" :ratings="food.ratings" @ratingtype-select="ratingselect" @ratingtype-toggle="ratingtoggle"></ratingselect>
                <div class="rating-wrapper">
                    <ul v-show="food.ratings">
                        <li v-show="needShow(rating.rateType,rating.text)" v-for="rating in food.ratings" :key="rating.username" class="rating-item">
                            <div class="user">
                                <span class="name">{{rating.username}}</span>
                                <img class="avatar" :src="rating.avatar">
                            </div>
                            <div class="time">{{rating.rateTime | formatDate}}</div>
                            <p class="text">
                                <span :class="{'icon-thumb_up':rating.rateType === 0,'icon-thumb_down':rating.rateType === 1}" class="icon"></span>
                                {{rating.text}}
                            </p>
                        </li>
                    </ul>
                    <div class="no-rating" v-show="!food.ratings">暂无评价</div>
                </div>
            </div>
        </div>
    </div>
  </transition>
</template>

<script>
import Vue from 'vue'
import BScroll from 'better-scroll'
import cartcontrol from '../cartcontrol/cartcontrol'
import split from '../split/split'
import ratingselect from '../ratingselect/ratingselect'
import { formatDate } from '../../common/js/date.js'

// const POSITIVE = 0
// const NEGATIVE = 1
const ALL = 2

export default {
  props: {
    food: {
      type: Object
    }
  },
  data() {
    return {
      showFlag: false,
      selectType: ALL,
      onlyContent: true,
      desc: {
          all: '全部',
          positive: '推荐',
          negative: '吐槽'
      }
    }
  },
  methods: {
    show() {
        this.showFlag = true
        // 每次新进入不同的商品详情页，都要初始化
        this.selectType = ALL
        this.onlyContent = false
        this.$nextTick(() => {
            if (!this.foodScroll) {
                this.foodScroll = new BScroll(this.$refs.foodscroll, {
                    click: true
                })
            } else {
                this.foodScroll.refresh()
            }
        })
    },
    hide() {
        this.showFlag = false
    },
    addFirst(event) {
        if (!event._constructed) {
            return
        }
        Vue.set(this.food, 'count', 1)
        this.$emit('cart-add', event.target)
    },
    cartAddFood(el) {
      this.$nextTick(() => {
        this.$emit('cart-add', el)
      })
    },
    needShow(type, text) {
        // 判断是否显示内容
        if (this.onlyContent && !text) {
            return false
        }
        if (this.selectType === ALL) {
            return true
        } else {
            // 对比type，相同的那个li才能显示
            return type === this.selectType
        }
    },
    ratingselect(type) {
        this.selectType = type
        // 异步更新一下DOM
        this.$nextTick(() => {
            this.foodScroll.refresh()
        })
    },
    ratingtoggle(onlyContent) {
        this.onlyContent = onlyContent
        this.$nextTick(() => {
            this.foodScroll.refresh()
        })
    }
  },
  filters: {
      formatDate(time) {
          let date = new Date(time)
          return formatDate(date, 'yyyy-MM-dd hh:mm')
      }
  },
  components: {
      cartcontrol,
      split,
      ratingselect
  }
}
</script>

<style lang='stylus' scoped>
@import '~common/stylus/mixin.styl'
@import '~common/stylus/variable.styl'

.move-enter-active,.move-leave-active
    transition: all 0.2s linear
    transform: translate3d(0, 0, 0)
.move-enter,.move-leave-to
    transform: translate3d(100%, 0, 0)
.food
    position: fixed
    top: 0
    left: 0
    bottom: 48px
    z-index: 30
    width: 100%
    overflow: hidden
    background-color: #fff
    .image-header
        position: relative
        width: 100%
        height: 0
        padding-top: 100%
        img
            position: absolute
            top: 0
            left: 0
            width: 100%
            height: 100%
        .back
            position: absolute
            top: 10px
            left: 0
            .icon-arrow_lift
                display: block
                padding: 10px
                font-size: 20px
                color: #fff
    .content
        position: relative
        padding: 18px
        .name
            height: 14px
            line-height: 14px
            font-size: 14px
            font-weight: 700
            color: $color-background
        .extra
            margin: 8px 0 18px 0
            height: 10px
            line-height: 10px
            font-size: 0
            color: rgb(147,153,159)
            .count
                font-size: 10px
                margin-right: 12px
            .rating
                font-size: 10px
        .price
            line-height: 24px
            font-weight: 700
            .now
                margin-right: 8px
                font-size: 14px
                font-weight: 700
                color: rgb(240,20,20)
                .price-icon
                    font-size: 10px
                    font-weight: normal
            .old
                text-decoration: line-through
                font-size: 10px
                color: rgb(147,153,159)
        .fade-enter-active,.fade-leave-active
            transition: all 0.2s linear
            opacity: 1
        .fade-enter,.fade-leave-to
            opacity: 0
        .cartcontrol-botton
            padding: 6px
            position: absolute
            right: 12px
            bottom: 12px
            z-index: 32
            .botton
                width: 74px
                height: 24px
                line-height: 24px
                text-align: center
                font-size: 10px
                color: #fff
                background-color: $color-blue
                border-radius: 24px
        .cartcontrol-wrapper
            position: absolute
            right: 12px
            bottom: 12px
    .info
        padding: 18px
        .title
            padding-bottom: 6px
            line-height: 14px
            font-size: 14px
            color: $color-background
        .text
            padding: 0 8px
            line-height: 24px
            font-size: 12px
            font-weight: 200
            color: $color-gray
    .rating
        padding: 18px 0
        .title
            margin-left: 18px
            line-height: 14px
            font-size: 14px
            color: $color-background
        .rating-wrapper
            padding: 0 18px
            .rating-item
                position: relative
                padding: 16px 0
                border-1px($color-row-line)
                .user
                    position: absolute
                    top: 16px
                    right: 0
                    line-height: 12px
                    font-size: 0px
                    .name
                        margin-right: 6px
                        display: inline-block
                        vertical-align: top
                        font-size: 10px
                        color: $color-gray2
                    .avatar
                        width: 12px
                        height: 12px
                        border-radius: 50%
                .time
                    line-height: 12px
                    font-size: 10px
                    color: $color-gray2
                .text
                    margin-top: 6px
                    line-height: 24px
                    font-size: 12px
                    color: $color-background
                    .icon
                        margin-right: 4px
                        vertical-align: top
                        line-height: 24px
                        font-size: 12px
                        &.icon-thumb_up
                            color: $color-blue
                        &.icon-thumb_down
                            color: $color-gray2
            .no-rating
                padding: 16px 0
                font-size: 12px
                color: $color-gray2
</style>
